package it.unibz.ti2.internetPizzeria.servlets;

import it.unibz.ti2.internetPizzeria.dao.ProductDAO;
import it.unibz.ti2.internetPizzeria.dao.PropertyDAO;
import it.unibz.ti2.internetPizzeria.dao.UserDAO;

import it.unibz.ti2.internetPizzeria.entities.Customer;
import it.unibz.ti2.internetPizzeria.entities.Drink;
import it.unibz.ti2.internetPizzeria.entities.Pizza;
import it.unibz.ti2.internetPizzeria.entities.Property;
import it.unibz.ti2.internetPizzeria.utils.ServletUtil;
import it.unibz.ti2.internetPizzeria.webAttributes.UserInfo;

import java.io.IOException;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Listing available products and collecting information about new order like chosen pizzas, drinks and address to deliver
 */
public class NewOrder extends HttpServlet {

	private static final long serialVersionUID = -647408377383398736L;

	protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
			throws ServletException, IOException {
		this.doPost(arg0, arg1);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ProductDAO productDAO = null;
		try {
			productDAO = new ProductDAO();
			productDAO.openSession();
			PropertyDAO propertyDAO = new PropertyDAO();
			propertyDAO.openSession(productDAO);
			UserDAO userDAO = new UserDAO();
			userDAO.openSession(productDAO);

			SortedSet<Pizza> pizzas;
			pizzas = new TreeSet<Pizza>(productDAO.getPizzas(true));
			SortedSet<Drink> drinks;
			drinks = new TreeSet<Drink>(productDAO.getDrinks(true));

			UserInfo userInfo = (UserInfo) request.getSession().getAttribute(
					"userInfo");

			if (userInfo.getUserType().equals("Customer")) {
				Customer customer = (Customer) userDAO
						.getUser(userInfo.getId());
				if (customer != null) {
					String name = customer.getName();

					request.setAttribute("name", name);
					String surname = customer.getSurname();
					request.setAttribute("surname", surname);
					String city = customer.getCity();
					request.setAttribute("city", city);
					String streetAndNumber = customer.getStreetAndNumber();
					request.setAttribute("streetAndNumber", streetAndNumber);
					String country = customer.getCountry();
					request.setAttribute("country", country);
					String zipCode = customer.getZipCode();
					request.setAttribute("zipCode", zipCode);

					String propertyCountry = new String(propertyDAO
							.getProperty(Property.CountryKey).getValue());
					request.setAttribute("propertyCountry", propertyCountry);

					Float propertyDeliveryMaximumDistanceFromPizzeria = Float
							.parseFloat((propertyDAO
									.getProperty(Property.DeliveryMaximumDistanceFromPizzeriaKey)
									.getValue()));
					request.setAttribute("DeliveryMaximumDistanceFromPizzeria",
							propertyDeliveryMaximumDistanceFromPizzeria);

					Float propertyDeliveryFreeDistanceInMeters = Float
							.parseFloat((propertyDAO
									.getProperty(Property.DeliveryFreeDistanceInMetersKey)
									.getValue()));
					request.setAttribute("DeliveryFreeDistanceInMeters",
							propertyDeliveryFreeDistanceInMeters);

					Float propertyDeliveryFeeForEveryIntervalInEuro = Float
							.parseFloat((propertyDAO
									.getProperty(Property.DeliveryFeeForEveryIntervalInEuroKey)
									.getValue()));
					request.setAttribute("DeliveryFeeForEveryIntervalInEuro",
							propertyDeliveryFeeForEveryIntervalInEuro);

					Float propertyDeliveryFeeIntervalInMeters = Float
							.parseFloat((propertyDAO
									.getProperty(Property.DeliveryFeeIntervalInMetersKey)
									.getValue()));
					request.setAttribute("DeliveryFeeIntervalInMeters",
							propertyDeliveryFeeIntervalInMeters);

					request.setAttribute("isCustomer", new Boolean(true));
				} else {
					request.setAttribute("isCustomer", new Boolean(false));
				}
			} else {
				request.setAttribute("isCustomer", new Boolean(false));
			}

			request.setAttribute("pizzas", pizzas);
			request.setAttribute("drinks", drinks);
			Integer maxAmountOfEachProductInOrder = new Integer(propertyDAO
					.getProperty(
							Property.MaxAmountOfEachProductInOrderPropertyKey)
					.getValue());
			request.setAttribute("max_amount", maxAmountOfEachProductInOrder);
			request.setAttribute("pizzeriaAddress", propertyDAO.getProperty(
					Property.PizzeriaAddressKey).getValue());

			ServletUtil.redirect(request, response, "/newOrder.jsp");
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (productDAO != null) {
				productDAO.closeSession();
			}
		}

	}
}
